package com.example.yangguoying;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.yangguoying.datasource.DataSource;
import com.example.yangguoying.util.JDBC.JDBCAPI.JDBCUtilImpl;

import java.sql.ResultSet;
import java.sql.SQLException;

public class RegisterActivity extends AppCompatActivity {

    DataSource dataSource;
    JDBCUtilImpl jdbcUtil;
    Button register_button;
    ImageButton bt_back;
    EditText register_account;
    EditText register_password;
    EditText register_re_password;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        setView();
    }
    private void initView() {
        register_button = findViewById(R.id.register_button);
        bt_back = findViewById(R.id.bt_back);
        register_account = findViewById(R.id.register_account);
        register_password = findViewById(R.id.register_password);
        register_re_password = findViewById(R.id.register_re_password);
        dataSource = new DataSource();
        jdbcUtil = new JDBCUtilImpl(dataSource.getConnectionUrl(),dataSource.getDatabaseUser(),dataSource.getDatabasePassword(),"com.mysql.jdbc.Driver");
        progressDialog = new ProgressDialog(RegisterActivity.this);
    }

    private void setView() {

        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerUser();
            }
        });

        bt_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }



    private String getAccount(){
        return register_account.getText().toString();
    }

    private String getPassword(){
        return register_password.getText().toString();
    }

    private String getRePassword(){
        return register_re_password.getText().toString();
    }
    private void registerUser() {
        String account = getAccount();
        String password = getPassword();
        String rePassword = getRePassword();
        progressDialog.setTitle("注册中....");
//设置ProgressDialog 是否可以按返回键取消；
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
//显示ProgressDialog
        progressDialog.show();
        if (!account.equals("") && !password.equals("") && !rePassword.equals("")){
            if (password.equals(rePassword)){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String querySql = "SELECT * FROM android_users where user_name = ?";
                        ResultSet resultSet = jdbcUtil.queryData(querySql,new Object[]{account});
                        try {
                            if (resultSet.next()){
                                showIsExist();
                            }else{
                                String insertSql ="INSERT INTO android_users(user_name,user_password) values (?,?)";
                                int count = jdbcUtil.insertData(insertSql,new Object[]{account,password});
                                //TODO
                                if (count == 1){
                                    showResponse();
                                }
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }else{
                Toast.makeText(RegisterActivity.this,"密码不一致！", Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(RegisterActivity.this,"参数不能为空！", Toast.LENGTH_SHORT).show();
        }
    }
    private void showResponse() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog.cancel();
                Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finish();
            }
        });
    }

    private void showIsExist() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog.cancel();
                Toast.makeText(RegisterActivity.this,"账号已经存在",Toast.LENGTH_SHORT).show();
            }
        });
    }
}